package com.wedo.mall.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.e.library.adapter.Adapter;
import com.e.library.http.EApi;
import com.e.library.http.EClient;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.wedo.mall.R;
import com.wedo.mall.activity.user.MyAddressActivity;
import com.wedo.mall.dialog.ConsultDialog;
import com.wedo.mall.dialog.SimpleDialog;
import com.wedo.mall.entity.MyAddress;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;

/**
 * Created by Lawrence on 2017/7/14.
 */

public class MyAddressAdapter extends Adapter<MyAddress> {

    MyAddressActivity context;

    public MyAddressAdapter(MyAddressActivity context, List<MyAddress> myAddresses) {
        super(context, myAddresses);
        this.context = context;
    }

    @Override
    protected int layout() {
        return R.layout.item_myaddress;
    }

    @Override
    protected void bind(EHolder holder, final MyAddress myAddress, final int position) {
        TextView tv_name = holder.find(R.id.tv_name);
        TextView tv_number = holder.find(R.id.tv_number);
        TextView tv_address = holder.find(R.id.tv_address);
        TextView tv_default = holder.find(R.id.tv_default);
        RelativeLayout rl_myaddress = holder.find(R.id.rl_myaddress);

        tv_name.setText(myAddress.consignee);
        tv_number.setText(myAddress.telephone);

        if(myAddress.city_name.equals(myAddress.county_name)){
            tv_address.setText(myAddress.county_name + myAddress.address);
        }else if(myAddress.province_name.equals(myAddress.city_name)){
            tv_address.setText(myAddress.city_name + myAddress.county_name + myAddress.address);
        }else{
            tv_address.setText(myAddress.province_name + myAddress.city_name + myAddress.county_name + myAddress.address);
        }

        if(TextUtils.equals("1",myAddress.is_default)){
            tv_default.setText(R.string.user_defaultaddress);
            tv_default.setSelected(false);
            tv_address.setEnabled(false);
            tv_address.setOnClickListener(null);
        }else {
            tv_default.setText(R.string.user_setdefault);
            tv_default.setSelected(true);
            setDefault(tv_default,myAddress);
        }

        rl_myaddress.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                SimpleDialog dialog = SimpleDialog.with(context);
                dialog.setQuestion(context.getString(R.string.deleteAddress));
                dialog.listener = new SimpleDialog.OnEnsureListener() {
                    @Override
                    public void onEnsure() {
                        delConsignee(myAddress.getId());
                    }
                };
                dialog.show();
                return true;
            }
        });

        rl_myaddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(onItemClickListener !=null) {
                    onItemClickListener.onInvoked(v,myAddress,position);
                }
            }
        });
    }

    private void setDefault(TextView tv_default, final MyAddress myAddress){

        tv_default.setEnabled(true);
        tv_default.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Map<String,String> params = new HashMap<>();
                params.put("id",myAddress.id);
                Observable<EResponse> observable = EClient.with().api().post(EApi.USER_DEFAULT,params);
                EClient.with().observer(observable, new EObserver() {
                    @Override
                    public void successful(EResponse response) {
                        context.refresh();
                    }

                    @Override
                    public void failure(String msg) {
                        Toast.makeText(mContext,msg,Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    private void delConsignee(String id){

        Map<String,String> params = new HashMap<>();
        params.put("id",id);
        Observable<EResponse> observable = EClient.with().api().post(EApi.USER_DELCONSIGNES,params);
        EClient.with().observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                context.refresh();
            }

            @Override
            public void failure(String msg) {
                Toast.makeText(mContext,msg,Toast.LENGTH_SHORT).show();
            }
        });
    }

}
